using System;
using System.Collections.Generic;
using System.Drawing;
using PdfSharp.Drawing;

namespace DeltaFlex.Eines
{
    public class PDFWaterMark
    {
        #region Var / Prop

        XFont fnt;

        private string _text;
        private PDFText _pdfText;
        private WaterMarkType _type;

        public string Text
        {
            get { return this._text; }
            set { this._text = value; }
        }
        public WaterMarkType Type
        {
            get { return this._type; }
            set { this._type = value; }
        }
        public PDFText PdfText
        {
            get { return this._pdfText; }
            set { this._pdfText = value; }
        }
        #endregion

        #region Constructor
        
        public PDFWaterMark()
        {
            fnt = new XFont("Verdana", 30, XFontStyle.Bold);
        }

        /// <summary>
        /// Para la Version Multiline
        /// </summary>
        /// <param name="text"></param>
        public PDFWaterMark(string text)
        {
            this.Text = text;
            fnt = new XFont("Verdana", 30, XFontStyle.Bold);
        }

        /// <summary>
        /// Para la version Simple
        /// </summary>
        /// <param name="text"></param>
        public PDFWaterMark(PDFText text)
        {
            this.PdfText = text;
        }
        #endregion

        #region Metodos

        internal XSize MeasureText(XGraphics gfx, string text)
        {
            if (text == null)
            {
                return gfx.MeasureString(string.Empty, fnt);
            }
            else
            {
                if (text.Length > 0 && text[text.Length - 1] == ' ')
                    return gfx.MeasureString(" " + text, fnt);
                else
                    return gfx.MeasureString(text, fnt);
            }
        }

        public void Paint(XGraphics gfx)
        {
            gfx.RotateTransform(55);

            double diagonalLength = Math.Sqrt(Math.Pow(gfx.PageSize.Width, 2) + Math.Pow(gfx.PageSize.Height, 2));

            int xVal = 0;
            int yVal = 0;

            switch (_type)
            {
                case WaterMarkType.Multiline:
                    {
                        string text = this._text;
                        XSize size = MeasureText(gfx, text);
                        xVal = 0;
                        yVal = -400;
                        while (size.Width < diagonalLength)
                        {
                            text += " " + this._text;
                            size = MeasureText(gfx, text);
                        }

                        for (int i = 0; i < 10; i++)
                        {
                            gfx.DrawString(text, fnt, XBrushes.Lavender, xVal, yVal);
                            xVal -= 20;
                            yVal += 120;
                        }
                    }
                    break;
                case WaterMarkType.Simple:
                    {
                        xVal = 0;
                        yVal = -20;

                        this._pdfText.Paint(gfx, new XRect(new RectangleF(xVal, yVal, (float)(diagonalLength - 30), 200)));
                    }
                    break;
                default:
                    break;
            }          

            gfx.RotateTransform(-55);
        }

        #endregion
    }
}
